package org.road0329.dlt698.axdr;


import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;

public class ConnectResponse extends AxdrType {
    private ByteBuffer dataCode = null;
    private PIID piid_acd = null;
    private FactoryVersion fver = null;
    private Unsigned16 wver = null;
    private ProtocolConformance wpro = null;
    private FunctionConformance wfun = null;
    private Unsigned16 wSendSize = null;
    private Unsigned16 wRecvSize = null;
    private Unsigned8 wRecvWnd = null;
    private Unsigned16 wMaxApdu = null;
    private Unsigned32 wMaxIdle = null;
    private ConnectResponseInfo wConnObj = null;

    public void setDataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void set_all(PIID piid_acd, FactoryVersion fver, Unsigned16 wver, ProtocolConformance wpro,
                        FunctionConformance wfun, Unsigned16 wSendSize, Unsigned16 wRecvSize, Unsigned8 wRecvWnd,
                        Unsigned16 wMaxApdu, Unsigned32 wMaxIdle, ConnectResponseInfo wConnObj) {
        this.piid_acd = piid_acd;
        this.fver = fver;
        this.wver = wver;
        this.wpro = wpro;
        this.wfun = wfun;
        this.wSendSize = wSendSize;
        this.wRecvSize = wRecvSize;
        this.wRecvWnd = wRecvWnd;
        this.wMaxApdu = wMaxApdu;
        this.wMaxIdle = wMaxIdle;
        this.wConnObj = wConnObj;
    }

    public void setPIID_ACD(PIID piid_acd) {
        this.piid_acd = piid_acd;
    }

    public void setFver(FactoryVersion fver) {
        this.fver = fver;
    }

    public void setWver(Unsigned16 wver) {
        this.wver = wver;
    }

    public void setWpro(ProtocolConformance wpro) {
        this.wpro = wpro;
    }

    public void setWfun(FunctionConformance wfun) {
        this.wfun = wfun;
    }

    public void setWSendSize(Unsigned16 wSendSize) {
        this.wSendSize = wSendSize;
    }

    public void setWRecvSize(Unsigned16 wRecvSize) {
        this.wRecvSize = wRecvSize;
    }

    public void setWRecvWnd(Unsigned8 wRecvWnd) {
        this.wRecvWnd = wRecvWnd;
    }

    public void setWMaxApdu(Unsigned16 wMaxApdu) {
        this.wMaxApdu = wMaxApdu;
    }

    public void setWMaxIdle(Unsigned32 wMaxIdle) {
        this.wMaxIdle = wMaxIdle;
    }

    public void setWConnObj(ConnectResponseInfo wConnObj) {
        this.wConnObj = wConnObj;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piid_acd = new PIID();
        this.piid_acd.setNumBit(8);
        codeLength += this.piid_acd.decode(input);
        this.fver = new FactoryVersion();
        codeLength += this.fver.decode(input);
        this.wver = new Unsigned16();
        this.wver.setConst();
        codeLength += this.wver.decode(input);
        this.wpro = new ProtocolConformance();
        codeLength += this.wpro.decode(input);
        this.wfun = new FunctionConformance();
        codeLength += this.wfun.decode(input);
        this.wSendSize = new Unsigned16();
        this.wSendSize.setConst();
        codeLength += this.wSendSize.decode(input);
        this.wRecvSize = new Unsigned16();
        this.wRecvSize.setConst();
        codeLength += this.wRecvSize.decode(input);
        this.wRecvWnd = new Unsigned8();
        this.wRecvWnd.setConst();
        codeLength += this.wRecvWnd.decode(input);
        this.wMaxApdu = new Unsigned16();
        this.wMaxApdu.setConst();
        codeLength += this.wMaxApdu.decode(input);
        this.wMaxIdle = new Unsigned32();
        this.wMaxIdle.setConst();
        codeLength += this.wMaxIdle.decode(input);
        this.wConnObj = new ConnectResponseInfo();
        codeLength += this.wConnObj.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.capacity();
            for (int i = this.dataCode.capacity() - 1; i >= 0; i--) {
                output.write(this.dataCode.get(i));
            }
        } else {
            if (this.wConnObj != null && this.wMaxIdle != null && this.wMaxApdu != null && this.wRecvWnd != null
                    && this.wRecvSize != null && this.wSendSize != null && this.wfun != null && this.wpro != null
                    && this.wver != null && this.piid_acd != null && this.fver != null) {
                codeLength = 0;
                codeLength += this.wConnObj.encode(output);
                codeLength += this.wMaxIdle.encode(output);
                codeLength += this.wMaxApdu.encode(output);
                codeLength += this.wRecvWnd.encode(output);
                codeLength += this.wRecvSize.encode(output);
                codeLength += this.wSendSize.encode(output);
                codeLength += this.wfun.encode(output);
                codeLength += this.wpro.encode(output);
                codeLength += this.wver.encode(output);
                codeLength += this.fver.encode(output);
                codeLength += this.piid_acd.encode(output);
            } else {
                throw new Exception(
                        "wConnObj || wMaxIdle || wMaxApdu || wRecvWnd || wRecvSize || wSendSize || wfun || wpro || wver || || fver || piid_acd is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    public String toString() {
        if (this.wConnObj != null && this.wMaxIdle != null && this.wMaxApdu != null && this.wRecvWnd != null
                && this.wRecvSize != null && this.wSendSize != null && this.wfun != null && this.wpro != null
                && this.wver != null && this.piid_acd != null && this.fver != null) {
            return "sequence: {" + "piid_acd: " + this.piid_acd + ", fver: " + this.fver + ", wver: " + this.wver
                    + ", wpro: " + this.wpro + ", wfun: " + this.wfun + ", wsendsize: " + this.wSendSize
                    + ", wrecvsize: " + this.wRecvSize + ", wrecvwnd: " + this.wRecvWnd + ", wmaxapdu: " + this.wMaxApdu
                    + ", wmaxidle: " + this.wMaxIdle + ", wconnobj: " + this.wConnObj + "}";
        } else {
            return "wConnObj || wMaxIdle || wMaxApdu || wRecvWnd || wRecvSize || wSendSize || wfun || wpro || wver || || fver || piid_acd is null";
        }
    }
}



